package com.ywgt;

import java.util.Scanner;

public class CirceQueue {
    public static void main(String[] args) {
        CircleQueue circleQueue=new CircleQueue(4);
        boolean loop=true;
        while(loop){
            System.out.println("输入a，添加队列");
            System.out.println("输入g, 取出数据");
            System.out.println("输入s，展示队列");
            System.out.println("输入e，退出系统");
            Scanner scanner=new Scanner(System.in);
            String next = scanner.next();

            switch (next){
                case "a":
                    System.out.println("请输入要添加的数");
                    int i = scanner.nextInt();
                    try {
                        circleQueue.add(i);
                        System.out.println("添加成功");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "g":
                    try {
                        int i1 = circleQueue.get();
                        System.out.println(i1);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "s":
                    try {
                        circleQueue.show();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "h":
                    try {
                        int head = circleQueue.head();
                        System.out.println(head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "e":
                    loop=false;
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }
}
class CircleQueue{

    public int maxLength;

    public int front;

    public int rear;

    public int[] array;

    public CircleQueue(int maxLength){
        this.maxLength=maxLength;
        this.array=new int[maxLength];
    }

    public boolean isEmpty(){

        return front==rear;
    }
    public boolean isFull(){
        return (rear+1)%maxLength==front;
    }
    public void add(int val){
        if(isFull()){
            throw new RuntimeException("队列已满");
        }
        array[rear]=val;
        rear=(rear+1)%maxLength;
    }
    public void  show(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i <front+size() ; i++) {
            System.out.printf("arra[%d]=%d \n",i%maxLength,array[i%maxLength]);
        }
    }

    public int size(){
        return (rear+maxLength-front)%maxLength;
    }
    public int get(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，没有数据");
        }
        int val=array[front];
        front=(front+1)%maxLength;
        return val;
    }
    public int head(){
        if(isEmpty()){
            throw new RuntimeException("对列空");
        }
        return array[front];
    }

}
